import { Node, Vec2 } from 'cc'

export class GestureUtils {
	/**
	 * 设置垂直滑动手势
	 * @param node 目标节点
	 * @param onSwipeUp 上滑回调
	 * @param onSwipeDown 下滑回调
	 * @param minDistance 最小滑动距离
	 */
	static setupVerticalSwipe(
		node: Node,
		onSwipeUp: () => void,
		onSwipeDown: () => void,
		minDistance: number = 30
	): void {
		const startPos = new Vec2()

		node.on(Node.EventType.TOUCH_START, event => {
			startPos.set(event.touch.getLocation())
		})

		node.on(Node.EventType.TOUCH_END, event => {
			const endPos = event.touch.getLocation()
			const deltaY = endPos.y - startPos.y

			if (Math.abs(deltaY) >= minDistance) {
				deltaY > 0 ? onSwipeUp() : onSwipeDown()
			}
		})
	}
}
